package com.tengju.user.domain.model.archive.sign;

import com.tengju.user.domain.model.archive.letax.IdentityType;
import com.tengju.user.domain.model.archive.letax.LetaxAccount;
import com.tengju.user.domain.model.archive.letax.LetaxStatus;
import com.tengju.user.domain.model.sign.License;
import com.tengju.user.domain.model.sign.LicenseType;
import com.tengju.user.domain.shared.DomainException;
import com.tengju.user.domain.shared.DomainExceptionCode;
import lombok.Value;

import java.util.Optional;

@Value
public class SignInfo {

    IdentityType identity;

    Sign sign;

    SelfCertification selfCertification;

    LetaxAccount letaxAccount;

    public boolean isSignedSuccess() {
        String isSign = Optional.ofNullable(sign).map(Sign::getIsSigned).orElse("no");

        Boolean subStatus = false;
        if (IdentityType.CERTIFIED_BUSINESS == identity
                || IdentityType.CERTIFIED_INDIVIDUAL == identity) {
            subStatus = SelfCertificationStatus.APPROVED == selfCertification.getStatus();
        } else if (IdentityType.REGISTER_SOHO == identity){
            subStatus = LetaxStatus.FINISHED == letaxAccount.getStatus();
        }

        return isSign.equals("yes") && subStatus;
    }
//
//    public String accountName() {
//        if (IdentityType.CERTIFIED_BUSINESS == identity
//                || IdentityType.CERTIFIED_INDIVIDUAL == identity) {
//            return Optional.ofNullable(selfCertification)
//                    .map(SelfCertification::getCompanyName).orElse("");
//        } else {
//            return Optional.ofNullable(letaxAccount)
//                    .map(LetaxAccount::getIdCardName).orElse("");
//        }
//    }

    public String personalDepAccountName() {
        if (IdentityType.CERTIFIED_BUSINESS == identity) {
            throw DomainException.error(DomainExceptionCode.SELF_NOT_SUPPORT_PERSONAL_ACCOUNT);
        } else if (IdentityType.CERTIFIED_INDIVIDUAL == identity) {
            return Optional.ofNullable(selfCertification)
                            .map(SelfCertification::getIdCardName).orElse("");
        } else {
            return Optional.ofNullable(letaxAccount)
                            .map(LetaxAccount::getIdCardName).orElse("");
        }
    }

    public String companyDepAccountName() {
        if (IdentityType.REGISTER_SOHO == identity) {
            throw DomainException.error(DomainExceptionCode.INVALID_DATA,"乐税个体不支持开立企业账户");
        } else {
            return Optional.ofNullable(selfCertification)
                    .map(SelfCertification::getCompanyName).orElse("");
        }
    }



    public License personalDepAccountLicense() {

        if (IdentityType.CERTIFIED_BUSINESS == identity) {
            return null;
        } else if (IdentityType.CERTIFIED_INDIVIDUAL == identity) {
            return new License(LicenseType.IDCARD,
                    Optional.ofNullable(selfCertification)
                            .map(SelfCertification::getIdCardNo).orElse(""));
        } else {
            return new License(LicenseType.IDCARD,
                    Optional.ofNullable(letaxAccount)
                            .map(LetaxAccount::getIdCardNo).orElse(""));
        }
    }


    public License companyDepAccountLicense() {

        if (IdentityType.REGISTER_SOHO == identity) {
            return null;
        } else {
            return new License(LicenseType.SOCIAL_CREDIT,
                    Optional.ofNullable(selfCertification)
                            .map(SelfCertification::getCompanyNo).orElse(""));
        }
    }

    public String legalName() {
        return Optional.ofNullable(selfCertification)
                .map(SelfCertification::getIdCardName).orElse("");
    }

    public License legalLicense() {
        return new License(LicenseType.IDCARD,
                Optional.ofNullable(selfCertification)
                        .map(SelfCertification::getIdCardNo).orElse(""));
    }
}
